<?php
defined('IN_TS') or die('Access Denied.');

switch($ts){

		case "":
			$voteid = intval($_GET['id']);
			$vote = $new['vote']->find('vote',array(
					'voteid'=>$voteid,
			));
			
			if($vote && $vote['ischeck']==0){
                tsNotice('投票信息审核中...');
			}

            if($vote==''){
                header ( "HTTP/1.1 404 Not Found" );
                header ( "Status: 404 Not Found" );
                $title = '404';
                include pubTemplate ( "404" );
                exit;
            }
			
			$vote['content'] = tsDecode($vote['content']);
			
			$vote['option']=$new['vote']->findAll('vote_option',array(
					'voteid'=>$voteid,
			));
			//现在时间
			$time = time();
			$endtime=strtotime($vote['endtime']);
			
			//关联用户
			$vote['user']=$new['vote']->find('user_info',array(
					'userid'=>$vote['userid'],
			),'username');
			//计算总共滴投票数目 
			$allCount = $new['vote']->findCount('vote_select',array(
					'voteid'=>$voteid,
			));
			$arrOption = $new['vote']->findAll('vote_option',array(
					'voteid'=>$voteid,
			));
	
			//计算总共选项数目
			$optionCount = $new['vote']->findCount('vote_option',array(
					'voteid'=>$voteid,
			));
			//处理
			$optionCount =$optionCount - 1;
			
			$arr =array();
			foreach($arrOption as $k=>$v){
					$arrOption[$k]['precent']=round($v['count_vote']/$allCount,2);
					$arr[]=$arrOption[$k]['precent'];
					$type = $v['type'];
			}
		
			//处理字符串
			$pp = join(',',$arr);
			
			
			//找出投票记录 
			$voteRecord = $new['vote']->findAll('vote_select',array(
					'voteid'=>$voteid,
			),'addtime desc',null,8);
			foreach($voteRecord as $k=>$v){
				//找出用户
				$voteRecord[$k]['user']=$new['vote']->find('user_info',array(
						'userid'=>$v['userid'],
				),'username');		
				//找出选项 
				$voteRecord[$k]['select'] = $new['vote']->find('vote_option',array(
						'optionid'=>$v['optionid'],														
				));
			}

			//遍历评论  、分页 
			$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
			$url = SITE_URL.'index.php?app=vote&ac=show&id='.$voteid.'&page=';
			$lstart = $page*10-10;
			$Comments = $new['vote']->findCount('vote_comment',array('voteid'=>$voteid,));
			$pageUrl = pagination($Comments, 10, $page, $url);
			$arrVoteComment = $new['vote']->findAll('vote_comment',array(
						'voteid'=>$voteid,
			),'addtime asc',null,$lstart.',10');
			
			foreach($arrVoteComment as $k=>$v){
				$arrVoteComment[$k]['comment'] = tsDecode($v['comment']);
				$arrVoteComment[$k]['user']= aac('user')->getOneUser($v['userid']);
			}
			//找出热门投票 
			$arrHotVote = $new['vote']->findAll('vote',array(
					'ischeck'=>1,
			),'count_comment desc',null,'10');
			$title = $vote['topic'];
			include template('show');
			break;
			
		case "do":
		
			$userid = aac('user')->isLogin();	
			
			$voteid = intval($_POST['voteid']);
			$optionid = $_POST['optionid'];

			
			$str = $new['vote']->find('vote_select',array(
					'voteid'=>$voteid,
					'userid'=>$userid,
			));
			
			if(empty($optionid)){
				tsNotice('至少要选一个！');
				die;
			}elseif($str){
				tsNotice('您已经投过票了!');	
				die;
			} 
			if(is_array($optionid)){
				foreach($optionid as $k=>$v){
						$count_vote = $new['vote']->findCount('vote_select',array(
							'optionid'=>$v,													
						));
						$new['vote']->create('vote_select',array(
						'userid'=>$userid,
						'optionid'=>$v,
						'voteid' =>$voteid,
						'addtime'=>date('Y-m-d H:i:s'),	
						));
						$new['vote']->update('vote_option',array(
								'optionid'=>$v,
						),array(
								'count_vote'=>$count_vote +1,							
						));
				}		
			}else{
			
			//计算选项数目
			$count_vote = $new['vote']->findCount('vote_select',array(
						'optionid'=>$optionid,													
			));
				//插入表中
			$new['vote']->create('vote_select',array(
						'userid'=>$userid,
						'optionid'=>$optionid,
						'voteid' =>$voteid,
						'addtime'=>date('Y-m-d H:i:s'),	
			));
			//投票
			$new['vote']->update('vote_option',array(
					'optionid'=>$optionid,
			),array(
					'count_vote'=>$count_vote +1,							
			));
			}
			header('Location:'.tsUrl('vote','show',array('id'=>$voteid)));
			break;

}

